package com.java3_20;

public class addTicket implements Runnable {
    private Ticket ticket;

    public addTicket() {

    }

    public addTicket(Ticket ticket) {
        this.ticket = ticket;
    }

    public void add() {
        synchronized (ticket) {
            while (ticket.getTicket() < 5) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                ticket.setTicket(ticket.getTicket() + 1);
                System.out.println("当前的票数为：" + ticket.getTicket());
                ticket.notifyAll();
            }
            try {
                ticket.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public void run() {
        while(true){
            add();
        }
    }
}

